package com.gydtrade.common.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.util.Calendar;

/**
 * @ClassName: SystemStaticParam
 * @Description: 系统配置参数
 * @author: wmsh
 * @Date: 2023-10-15 22:23
 * @Version: V1.0.0
 */
@Slf4j
@Component
@ConfigurationProperties(prefix = "system-static-param")
public class SystemStaticParam implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();

        log.info("--- start DefaultStartInitListrner contextInitialized --- {}", this);

        String contentPath = servletContext.getContextPath();
        this.setContentPath(contentPath);
        servletContext.setAttribute("contentPath", contentPath);
        servletContext.setAttribute("contentAffixPath", contentPath + SystemStaticParam.affixRequestStr);
        servletContext.setAttribute("systemTitle", SystemStaticParam.systemTitle);

        SystemStaticParam.affixRequestStr = SystemStaticParam.webDomain + contentPath + SystemStaticParam.affixRequestStr;
        servletContext.setAttribute("affixRequestStr", SystemStaticParam.affixRequestStr);
        servletContext.setAttribute("webDomain", SystemStaticParam.webDomain);

        servletContext.setAttribute("companyName", SystemStaticParam.companyName);
        servletContext.setAttribute("companyAddress", SystemStaticParam.companyAddress);
        servletContext.setAttribute("companyLink", SystemStaticParam.companyLink);
        servletContext.setAttribute("companyPhone", SystemStaticParam.companyPhone);
        servletContext.setAttribute("companyEmail", SystemStaticParam.companyEmail);
        servletContext.setAttribute("companyQQ", SystemStaticParam.companyQQ);
        servletContext.setAttribute("companyWeb", SystemStaticParam.companyWeb);
        servletContext.setAttribute("webRecord", SystemStaticParam.webRecord);
        servletContext.setAttribute("securityRecord", SystemStaticParam.securityRecord);
        servletContext.setAttribute("webKeywords", SystemStaticParam.webKeywords);
        servletContext.setAttribute("webDescription", SystemStaticParam.webDescription);
        servletContext.setAttribute("technicalCompany", SystemStaticParam.technicalCompany);
        servletContext.setAttribute("technicalCompanyWeb", SystemStaticParam.technicalCompanyWeb);
        servletContext.setAttribute("technicalSupport", SystemStaticParam.technicalSupport);

        this.setYear(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
        this.setMonth(String.valueOf(Calendar.getInstance().get(Calendar.MONTH) + 1));
        servletContext.setAttribute("year", SystemStaticParam.year);
        servletContext.setAttribute("month", SystemStaticParam.month);

        servletContext.setAttribute("copyright", SystemStaticParam.copyright);

        System.out.println("\n******************************************");
        System.out.println("contentPath ---> " + contentPath);
        System.out.println("systemTitle ---> " + SystemStaticParam.systemTitle);
        System.out.println("webDomain ---> " + SystemStaticParam.webDomain);
        System.out.println("fileSysPath ---> " + SystemStaticParam.fileSysPath);
        System.out.println("affixGroupFolder ---> " + SystemStaticParam.affixGroupFolder);
        System.out.println("affixRequestRule ---> " + SystemStaticParam.affixRequestRule);
        System.out.println("affixRequestStr ---> " + SystemStaticParam.affixRequestStr);
        System.out.println("accountLength ---> " + SystemStaticParam.accountLength);
        System.out.println("passwordLength ---> " + SystemStaticParam.passwordLength);
        System.out.println("orderPrefix ---> " + SystemStaticParam.orderPrefix);
        System.out.println("date ---> " + SystemStaticParam.year + "-" + SystemStaticParam.month);
        System.out.println("webRecord ---> " + SystemStaticParam.webRecord);
        System.out.println("securityRecord ---> " + SystemStaticParam.securityRecord);
        System.out.println("******************************************\n");

        log.info(String.format("---- stop DefaultStartInitListrner contextInitialized %s", this));
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info(String.format("DefaultStartInitListrner contextDestroyed %s", this));
    }

    /**
     * 系统根路径
     */
    public static String contentPath;

    /**
     * 系统标题
     */
    public static String systemTitle;

    /**
     * 公司名称
     */
    public static String companyName;

    /**
     * 公司网址
     */
    public static String companyWeb;

    /**
     * 公司地址
     */
    public static String companyAddress;

    /**
     * 联系ren
     */
    public static String companyLink;

    /**
     * 联系电话
     */
    public static String companyPhone;

    /**
     * 联系邮箱
     */
    public static String companyEmail;

    /**
     * 联系QQ
     */
    public static String companyQQ;

    /**
     * 网站备案号
     */
    public static String webRecord;

    /**
     * 公网安备案号
     */
    public static String securityRecord;

    /**
     * 网站关键字
     */
    public static String webKeywords;

    /**
     * 网站描述
     */
    public static String webDescription;

    /**
     * 技术公司
     */
    public static String technicalCompany;

    /**
     * 技术公司网站
     */
    public static String technicalCompanyWeb;

    /**
     * 技术支持
     */
    public static String technicalSupport;

    /**
     * 系统域名
     */
    public static String webDomain;

    /**
     * 系统文件存放路径
     */
    public static String fileSysPath;

    /**
     * 文件组路径
     */
    public static String affixGroupFolder;

    /**
     * 附件访问路径
     */
    public static String affixRequestRule;

    /**
     * 附件访问路径
     */
    public static String affixRequestStr;

    /**
     * 账号最小长度
     */
    public static Integer accountLength;

    /**
     * 密码最小长度
     */
    public static Integer passwordLength;

    /**
     * 订单号前缀
     */
    public static String orderPrefix;

    /**
     * 当前年份
     */
    public static String year;

    /**
     * 版权
     */
    public static String copyright;

    /**
     * 当前月份
     */
    public static String month;

    public void setSystemTitle(String systemTitle) {
        SystemStaticParam.systemTitle = systemTitle;
    }

    public void setCompanyName(String companyName) {
        SystemStaticParam.companyName = companyName;
    }

    public void setCompanyAddress(String companyAddress) {
        SystemStaticParam.companyAddress = companyAddress;
    }

    public void setCompanyLink(String companyLink) {
        SystemStaticParam.companyLink = companyLink;
    }

    public void setCompanyPhone(String companyPhone) {
        SystemStaticParam.companyPhone = companyPhone;
    }

    public void setCompanyEmail(String companyEmail) {
        SystemStaticParam.companyEmail = companyEmail;
    }

    public void setCompanyQQ(String companyQQ) {
        SystemStaticParam.companyQQ = companyQQ;
    }

    public void setCompanyWeb(String companyWeb) {
        SystemStaticParam.companyWeb = companyWeb;
    }

    public void setWebRecord(String webRecord) {
        SystemStaticParam.webRecord = webRecord;
    }

    public void setSecurityRecord(String securityRecord) {
        SystemStaticParam.securityRecord = securityRecord;
    }

    public void setWebKeywords(String webKeywords) {
        SystemStaticParam.webKeywords = webKeywords;
    }

    public void setWebDescription(String webDescription) {
        SystemStaticParam.webDescription = webDescription;
    }

    public void setTechnicalCompany(String technicalCompany) {
        SystemStaticParam.technicalCompany = technicalCompany;
    }

    public void setTechnicalCompanyWeb(String technicalCompanyWeb) {
        SystemStaticParam.technicalCompanyWeb = technicalCompanyWeb;
    }

    public void setTechnicalSupport(String technicalSupport) {
        SystemStaticParam.technicalSupport = technicalSupport;
    }

    public void setWebDomain(String webDomain) {
        SystemStaticParam.webDomain = webDomain;
    }

    public void setFileSysPath(String fileSysPath) {
        SystemStaticParam.fileSysPath = checkFilePath(fileSysPath);

        if (!StringUtils.isEmpty(fileSysPath)) {
            File file = new File(fileSysPath);
            if (!file.exists()) {
                if (file.mkdirs()) {
                    log.debug("mkdirs:" + fileSysPath);
                }
            }
        }
    }

    private String checkFilePath(String filePath){
        String path = filePath;
        String osName = System.getProperty("os.name");
        if(osName.startsWith("Windows")){
            if(!filePath.startsWith(":", 1)){
                path = "C:" + filePath;
            }
        }else if (osName.startsWith("Linux")){
            if(filePath.startsWith(":", 1)){
                path = filePath.substring(2, filePath.length());
            }
        }else if (osName.startsWith("Mac")){
            if(filePath.startsWith(":", 1)){
                path = filePath.substring(2, filePath.length());
            }
        }
        return path;
    }

    public void setAffixGroupFolder(String affixGroupFolder) {
        SystemStaticParam.affixGroupFolder = affixGroupFolder;
    }

    public void setAffixRequestRule(String affixRequestRule) {
        SystemStaticParam.affixRequestRule = affixRequestRule;
        SystemStaticParam.affixRequestStr = affixRequestRule.replaceAll("\\*", "");
    }

    public void setAccountLength(Integer accountLength) {
        SystemStaticParam.accountLength = accountLength;
    }

    public void setPasswordLength(Integer passwordLength) {
        SystemStaticParam.passwordLength = passwordLength;
    }

    public void setOrderPrefix(String orderPrefix) {
        SystemStaticParam.orderPrefix = orderPrefix;
    }

    public void setYear(String year) {
        SystemStaticParam.year = year;
        this.setCopyright(year);
    }

    public void setCopyright(String copyright) {
        int number = Integer.parseInt(copyright);
        if (number <= 2023) {
            SystemStaticParam.copyright = copyright;
        } else {
            SystemStaticParam.copyright = "2023-" + copyright;
        }
    }

    public void setMonth(String month) {
        if (Integer.valueOf(month) < 10) {
            month = "0" + month;
        }
        SystemStaticParam.month = month;
    }

    public void setContentPath(String contentPath) {
        SystemStaticParam.contentPath = contentPath;
    }
}
